<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Encrypt an S/MIME message</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="function.openssl-pkcs7-decrypt.html">openssl_pkcs7_decrypt</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.openssl-pkcs7-sign.html">openssl_pkcs7_sign</a></div>
 <div class="up"><a href="ref.openssl.html">OpenSSL 函数</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="function.openssl-pkcs7-encrypt" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">openssl_pkcs7_encrypt</h1>
  <p class="verinfo">(PHP 4 &gt;= 4.0.6, PHP 5)</p><p class="refpurpose"><span class="refname">openssl_pkcs7_encrypt</span> &mdash; <span class="dc-title">Encrypt an S/MIME message</span></p>

 </div>
 
 <div class="refsect1 description" id="refsect1-function.openssl-pkcs7-encrypt-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="type">bool</span> <span class="methodname"><strong>openssl_pkcs7_encrypt</strong></span>
    ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$infile</code></span>
   , <span class="methodparam"><span class="type">string</span> <code class="parameter">$outfile</code></span>
   , <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <code class="parameter">$recipcerts</code></span>
   , <span class="methodparam"><span class="type">array</span> <code class="parameter">$headers</code></span>
   [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$flags</code><span class="initializer"> = 0</span></span>
   [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$cipherid</code><span class="initializer"> = OPENSSL_CIPHER_RC2_40</span></span>
  ]] )</div>

  <p class="para rdfs-comment">
   <span class="function"><strong>openssl_pkcs7_encrypt()</strong></span> takes the contents of the
   file named <em><code class="parameter">infile</code></em> and encrypts them using an RC2
   40-bit cipher so that they can only be read by the intended recipients
   specified by <em><code class="parameter">recipcerts</code></em>.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.openssl-pkcs7-encrypt-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>
<em><code class="parameter">infile</code></em></dt>

     <dd>

      <p class="para">
      </p>
     </dd>

    
    
     <dt>
<em><code class="parameter">outfile</code></em></dt>

     <dd>

      <p class="para">
      </p>
     </dd>

    
    
     <dt>
<em><code class="parameter">recipcerts</code></em></dt>

     <dd>

      <p class="para">
       Either a lone X.509 certificate, or an array of X.509 certificates.
      </p>
     </dd>

    
    
     <dt>
<em><code class="parameter">headers</code></em></dt>

     <dd>

      <p class="para">
       <em><code class="parameter">headers</code></em> is an array of headers that
       will be prepended to the data after it has been encrypted.
      </p>
      <p class="para">
       <em><code class="parameter">headers</code></em> can be either an associative array
       keyed by header name, or an indexed array, where each element contains
       a single header line.
      </p>
     </dd>

    
    
     <dt>
<em><code class="parameter">flags</code></em></dt>

     <dd>

      <p class="para">
       <em><code class="parameter">flags</code></em> can be used to specify options that affect
       the encoding process - see <a href="openssl.pkcs7.flags.html" class="link">PKCS7
       constants</a>.
      </p>
     </dd>

    
    
     <dt>
<em><code class="parameter">cipherid</code></em></dt>

     <dd>

      <p class="para">
       One of <a href="openssl.ciphers.html" class="link">cipher constants</a>.
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.openssl-pkcs7-encrypt-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   成功时返回 <strong><code>TRUE</code></strong>， 或者在失败时返回 <strong><code>FALSE</code></strong>。
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.openssl-pkcs7-encrypt-changelog">
  <h3 class="title">更新日志</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>版本</th>
       <th>说明</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>5.0.0</td>
       <td>
        The <em><code class="parameter">cipherid</code></em> parameter was added.
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.openssl-pkcs7-encrypt-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-840">
    <p><strong>Example #1 <span class="function"><strong>openssl_pkcs7_encrypt()</strong></span> example</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;the&nbsp;message&nbsp;you&nbsp;want&nbsp;to&nbsp;encrypt&nbsp;and&nbsp;send&nbsp;to&nbsp;your&nbsp;secret&nbsp;agent<br />//&nbsp;in&nbsp;the&nbsp;field,&nbsp;known&nbsp;as&nbsp;nighthawk.&nbsp;&nbsp;You&nbsp;have&nbsp;his&nbsp;certificate<br />//&nbsp;in&nbsp;the&nbsp;file&nbsp;nighthawk.pem<br /></span><span style="color: #0000BB">$data&nbsp;</span><span style="color: #007700">=&nbsp;&lt;&lt;&lt;EOD<br /></span><span style="color: #DD0000">Nighthawk,<br /><br />Top&nbsp;secret,&nbsp;for&nbsp;your&nbsp;eyes&nbsp;only!<br /><br />The&nbsp;enemy&nbsp;is&nbsp;closing&nbsp;in!&nbsp;Meet&nbsp;me&nbsp;at&nbsp;the&nbsp;cafe&nbsp;at&nbsp;8.30am<br />to&nbsp;collect&nbsp;your&nbsp;forged&nbsp;passport!<br /><br />HQ<br /></span><span style="color: #007700">EOD;<br /><br /></span><span style="color: #FF8000">//&nbsp;load&nbsp;key<br /></span><span style="color: #0000BB">$key&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">file_get_contents</span><span style="color: #007700">(</span><span style="color: #DD0000">"nighthawk.pem"</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;save&nbsp;message&nbsp;to&nbsp;file<br /></span><span style="color: #0000BB">$fp&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">fopen</span><span style="color: #007700">(</span><span style="color: #DD0000">"msg.txt"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"w"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">fwrite</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$data</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">fclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;encrypt&nbsp;it<br /></span><span style="color: #007700">if&nbsp;(</span><span style="color: #0000BB">openssl_pkcs7_encrypt</span><span style="color: #007700">(</span><span style="color: #DD0000">"msg.txt"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"enc.txt"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$key</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;array(</span><span style="color: #DD0000">"To"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"nighthawk@example.com"</span><span style="color: #007700">,&nbsp;</span><span style="color: #FF8000">//&nbsp;keyed&nbsp;syntax<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"From:&nbsp;HQ&nbsp;&lt;hq@example.com&gt;"</span><span style="color: #007700">,&nbsp;</span><span style="color: #FF8000">//&nbsp;indexed&nbsp;syntax<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"Subject"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"Eyes&nbsp;only"</span><span style="color: #007700">)))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;message&nbsp;encrypted&nbsp;-&nbsp;send&nbsp;it!<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">exec</span><span style="color: #007700">(</span><span style="color: #0000BB">ini_get</span><span style="color: #007700">(</span><span style="color: #DD0000">"sendmail_path"</span><span style="color: #007700">)&nbsp;.&nbsp;</span><span style="color: #DD0000">"&nbsp;&lt;&nbsp;enc.txt"</span><span style="color: #007700">);<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
 </div>


</div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="function.openssl-pkcs7-decrypt.html">openssl_pkcs7_decrypt</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.openssl-pkcs7-sign.html">openssl_pkcs7_sign</a></div>
 <div class="up"><a href="ref.openssl.html">OpenSSL 函数</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
